﻿
function openResourceEdit(task, field, el, callback)
{
    $(el).blur();

    // get editor
    var editor = getResourceEditor(task, field, callback);
    AttachEvents(editor);

    //display
    var offset = el.offset();
    editor.css({ left: offset.left, top: offset.top });
    editor.show();
}

function getResourceEditor(task, property, callback)
{
    $('.REditor').remove();
    var c = $("<div class='REditor'>");

    if (!task.Resources || !task.Resources.length)
        task.Resources = [{ New: true, ID: { Value: null }, Resource: { Id: { Value: user.ID.Value, TypeName: "User" }, Name: user.Name}}];
    if (task.Resources[0].New)
        task.Resources[0][property] = task[property];

    var index = 0;
    for (var i = 0; i < task.Resources.length; i++)
    {
        var contr = task[property].Unit > -1 ? new T_Duration() : new T_Double();

        if (user.ID.Value == task.Manager.Id.Value || user.ID.Value == task.Resources[i].Resource.Id.Value)
        {
            var resourceFieldName = stringFormat("resources[{0}]", index++);
            var propFieldName = stringFormat("{0}.{1}", resourceFieldName, property == "ActualEffort" ? "ActualRegularEffort" : property);
            var controls = contr.getControls("na", propFieldName, task.Resources[i][property], null, null, 20, true);
            var div = $("<div>").append(controls).append(" - " + task.Resources[i].Resource.Name);

            div.append(stringFormat("<input name='taskID' type='hidden' value='{0}'/>", task.ID.Value));

            if (!task.Resources[i].New)
                div.append(stringFormat("<input name='{0}.ID.Value' type='hidden' value='{1}'/>", resourceFieldName, task.Resources[i].ID.Value));
            else div.append(stringFormat("<input name='{0}.WorkItem.Id.TypeName' type='hidden' value='Task'/>\
                                          <input name='{0}.WorkItem.Id.Value' type='hidden' value='{1}'/>\
                                          <input name='{0}.Resource.Id.TypeName' type='hidden' value='User'/>\
                                          <input name='{0}.Resource.Id.Value' type='hidden' value='{2}'/>", resourceFieldName, task.ID.Value, user.ID.Value));
            c.append(div);
        }
        else
            c.append(stringFormat("<div>{0} - {1}</div>", contr.getText(task.Resources[i][property])
                                                        , task.Resources[i].Resource.Name))
    }

    c.append(stringFormat("<div class='Overall'> {0} - overall</div>", contr.getText(task[property])));

    if (task.Resources[0].New)
    {
        var div = $("<div class='Notification'>");
        div.append("You will be assigned as a resource once a % complete is entered");
        c.append(div);

        task.Resources = null;
    }
    var btnSave = $("<input type='button' value='ok'/>");
    var btnCancel = $("<input type='button' value='cancel'/>");
    var btnContainer = $("<div>");

    btnContainer.append(btnSave);
    btnContainer.append(btnCancel);
    c.append(btnContainer);

    btnCancel.click(function () { $('.REditor').remove(); });
    btnSave.click(function ()
    {
        var data = $('.REditor input, .REditor select').serialize();

        remoteRequest({
            url: baseUrl + '/Resources/Update',
            contentType: "JSON",
            type: "POST",
            data: data,
            success: function (res)
            {
                if (res.Data)
                {
                    $('.REditor').remove();
                    if (callback)
                        callback(res.Data);
                }
                else
                    alert(res.ErrorMessage);
            }
        });
    });
    var stopPropagation = function (e) { e.stopPropagation(); };
    c.click(stopPropagation).keydown(stopPropagation);
    $('body').append(c).click(function () { $('.REditor').remove(); });
    return c;
}
